﻿define([
    'dojo/_base/declare',
    'esri/SpatialReference',
    'esri/tasks/ProjectParameters',
    'geomap/plugins/layers/ExGraphicsLayer',
    'esri/symbols/CartographicLineSymbol',
    'esri/symbols/SimpleMarkerSymbol',
    'esri/geometry/Point',
    'esri/graphic',
], function (
    declare,
    SpatialReference,
    ProjectParameters,
    GraphicsLayer,
    CartographicLineSymbol,
    SimpleMarkerSymbol,
    Point,
    Graphic
) {
    var PointLayer = declare([GraphicsLayer], {
        setData: function (mapUtils, layer) {
            var me = this;

            me.clear();
            if (!mapUtils) {
                return;
            }
            me.mapUtils = mapUtils;
            me._setLayer(layer);
            var options = layer.layer || {};
            me.options = options;
            var data = Object.assign([], options.data);

            if (options.isClickReorderGraphic != undefined) {
                options.isClickReorderGraphic = options.isClickReorderGraphic;
            } else {
                options.isClickReorderGraphic = true;
            }
            if (options.isClickReorderLayer != undefined) {
                options.isClickReorderLayer = options.isClickReorderLayer;
            } else {
                options.isClickReorderLayer = true;
            }

            me.popupOptions = Object.assign(
                {
                    visible: false,
                    isMultiPopup: false,
                    contentField: 'content',
                    isShowCloseIcon: false,
                },
                options.popupOptions
            );

            me.highlightOptions = Object.assign(
                {
                    sizeFactor: 1.2,
                    widthFactor: 1.2,
                    heightFactor: 1.2,
                    visible: false,
                },
                options.highlightOptions
            );

            me.__currentGraphics = [];
            if (me.__mapOnLoadHandle) {
                clearInterval(me.__mapOnLoadHandle);
            }
            me.__mapOnLoadHandle = setInterval(function () {
                if (mapUtils.getMap().loaded) {
                    clearInterval(me.__mapOnLoadHandle);

                    if (!data || data.length < 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    } else {
                        // var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
                        var symbolFieldName = options.symbolFieldName || 'symbol';
                        var spatialReference = me.spatialReference;
                        var lngFieldName = options.lngFieldName || 'x';
                        var latFieldName = options.latFieldName || 'y';
                        if (options.outSr) {
                            var PrjParams = new ProjectParameters();
                            PrjParams.outSR = new SpatialReference({ wkid: options.outSr });
                            PrjParams.geometries = [];
                            lngLatArray.map(function (lngLat, index) {
                                var point = mapUtils.createPoint(mapView, lngLat, {
                                    sr: spatialReference,
                                });
                                PrjParams.geometries.push(point);
                            });
                            mapView.geometryService.project(PrjParams, function (outputPoints) {
                                console.log(outputPoints);
                                lngLatArray.map(function (lngLat, index) {
                                    var symbol =
                                        lngLat[symbolFieldName] || options.symbol || 'common_point';
                                    mapUtils.getSymbol(mapView, symbol, function (symbol) {
                                        var attributes = lngLat;
                                        if (lngLat.x > 0 && lngLat.y > 0) {
                                            var graphic = mapUtils.createGraphic(mapView, {
                                                geometry: outputPoints[index],
                                                symbol: symbol,
                                                attributes: attributes,
                                            });
                                            me.add(graphic);
                                        }
                                        if (index == lngLatArray.length - 1) {
                                            me._calCurrentGraphics();
                                            if (options.labelOptions) {
                                                me.labelOptions = mapUtils.$.extend(
                                                    true,
                                                    {
                                                        textField: 'name',
                                                        ptThreshold: 0, //当前窗口的点位少于这个最大值时，标签强制展示
                                                        showLabels: 1, //1:根据比例尺展示，2：强制展示，0：强制不展示
                                                        scaleInfo: {
                                                            maxScale: 0,
                                                            minScale: 0,
                                                        },
                                                        labelSymbol: {
                                                            labelClass: 'labelInfo',
                                                            labelStyle: {
                                                                offsety: 18,
                                                                offsetx: 0,
                                                                position: 'top',
                                                            },
                                                        },
                                                    },
                                                    options.labelOptions
                                                );
                                                debugger;
                                                me._showLabels(mapView, options);
                                            }
                                            if (typeof options.callback == 'function') {
                                                options.callback(layer);
                                            }
                                        }
                                    });
                                });
                            });
                        } else {
                            data.map(function (item, index) {
                                if (lngFieldName && latFieldName) {
                                    item.x = item[lngFieldName];
                                    item.y = item[latFieldName];
                                }
                                var attributes = item;
                                var symbolItem = item[symbolFieldName] || options.symbol;
                                let symbol = me.createSymbol(symbolItem);
                                if (item.x > 0 && item.y > 0) {
                                    var point = me.createPoint(item, {
                                        sr: spatialReference,
                                    });

                                    var graphic = me.createGraphic({
                                        geometry: point,
                                        symbol: symbol,
                                        attributes: attributes,
                                    });
                                    me.add(graphic);
                                }
                                if (index == data.length - 1) {
                                    me._calCurrentGraphics();
                                    if (options.labelOptions) {
                                        me.labelOptions = Object.assign(
                                            {
                                                textField: 'name',
                                                ptThreshold: 0, //当前窗口的点位少于这个最大值时，标签强制展示
                                                showLabels: 1, //1:根据比例尺展示，2：强制展示，0：强制不展示
                                                scaleInfo: {
                                                    maxScale: 0, // 1000000,
                                                    minScale: 0,
                                                },
                                                labelSymbol: {
                                                    labelClass: 'labelInfo',
                                                    labelStyle: {
                                                        offsety: 18,
                                                        offsetx: 0,
                                                        position: 'top',
                                                    },
                                                },
                                            },
                                            options.labelOptions
                                        );

                                        me._showLabels(mapUtils, options);
                                    }
                                    if (typeof options.callback == 'function') {
                                        options.callback(layer);
                                    }
                                }
                            });
                        }

                        if (options.popupOptions && options.popupOptions.visible) {
                            me.openCustomPopup(mapUtils, lngLatArray);
                        }
                        // if (options.highlightOptions && options.highlightOptions.visible) {
                        me._showHighlight(mapUtils, options);
                        // }
                        // ;
                    }
                }
            });
        },
        getGraphicLocation: function (mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            if (!mapView) {
                return;
            }
            var map = mapView.getMap();
            if (!map) {
                return;
            }
            var geometry = esriEvent.graphic.geometry || {};
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            location.sr = geometry.spatialReference;

            return location;
        },
        createSymbol(t = { type: null }) {
            if (t.type == 'esriSMS') {
                return new SimpleMarkerSymbol(t);
            } else if (t.type == 'esriPMS') {
                return new PictureMarkerSymbol(t);
            } else {
                let symbol = new SimpleMarkerSymbol({
                    type: 'esriSMS',
                    style: 'esriSMSSquare',
                    color: [76, 115, 0, 255],
                    size: 6,
                    angle: 0,
                    xoffset: 0,
                    yoffset: 0,
                    outline: {
                        color: [152, 230, 0, 255],
                        width: 2,
                    },
                });
                return symbol;
            }
        },
        createPoint(t, a = {}) {
            var o = a.lngFieldName || 'x',
                i = a.latFieldName || 'y',
                n = t.sr || a.sr || this.mapUtils.getSpatialReference();
            let g = new Point(+t[o], +t[i], n);
            return g;
        },
        createGraphic(t) {
            t = Object.assign(
                { geometry: null, symbol: null, attributes: null, infoTemplate: null },
                t
            );
            let g = new Graphic(t.geometry, t.symbol, t.attributes, t.infoTemplate);
            return g;
        },
    });
    return PointLayer;
});
